// sonPage/person/individual/individual.js
import personRequire from '../../../utils/person.js'
import Notify from '../../../@vant/notify/notify'
const getUserMsg = new personRequire()
const updateMsg = new personRequire()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nickName:'',
    brief:'',
    sending:false,
    avatar:"https://img.yzcdn.cn/vant/cat.jpeg"
  },
  afterRead(event) {
    let that = this
    const { file } = event.detail;
    console.log(file)
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      url: 'https://qichao.top:7443/MES/user/uploadResource',
      filePath: file.url,
      name: 'file',
      header:{
        "token":wx.getStorageSync('token')
      },
      success(res) {
        // 上传完成需要更新 fileList
        let data = JSON.parse(res.data)
        that.setData({
          avatar:data.obj
        })
        console.log(data)
        updateMsg.updateUserMsg(that.data.nickName,that.data.brief,data.obj,wx.getStorageSync('token')).then(code=>{
          if(code === 200){
            Notify({ type: 'success', message: '修改成功' })
          }
        })
      },
    });
  },
  sendingMsg(){
    //先修改上传状态为true
    this.setData({
      sending:true
    })
    let that = this
    updateMsg.updateUserMsg(that.data.nickName,that.data.brief,that.data.avatar,wx.getStorageSync('token')).then(code=>{
      if(code === 200){
        Notify({ type: 'success', message: '修改成功' })
      }
    })
    this.setData({
      sending:false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(wx.getStorageSync('token')){
      this.setData({isLogin:true})
      getUserMsg.getUserMsg(wx.getStorageSync('token')).then(res=>{
        console.log(res)
        let data = res.data.obj
        this.setData({
          nickName:data.nick,
          brief:data.introduce,
          avatar:data.headname
        })
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})